package com.klhappy.houyuan.fragement.dingdanfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.activity.ConfirmOrderActivity;
import com.klhappy.houyuan.adapter.SendAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.GoodsAllBean;
import com.klhappy.houyuan.bean.SendBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.DingDanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.AbsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by MrPu on 15/12/12.
 */
public class SendFragment extends AbsFragment implements CallBackInterModle {
    public static final String PNG = "SendFragment";
    //待派送
    @InjectView(R.id.send_lv)
    ListView send_lv;

    private DingDanModle modle;
    private SendBean sendBean;
    private List<SendBean.Records> records;
    private SendAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dingdan_send, container, false);
        ButterKnife.inject(this, view);

        modle = new DingDanModle(getActivity());
        modle.setCallBackInterModle(this);

        NotifyMethod.getInstance().addObserver(PNG, new NotifyMethod.NotifyListener() {
            @Override
            public void notify(Object o) {
                modle.setDan((String) o);
                showDialog("下单中...");
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        modle.getSend();
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.ORDER_LIST_URL)){
            if (o.getCode() == 200){
                sendBean = JsonCode.getClass(o.getEntity(), SendBean.class);
                records = JsonCode.getClassList(sendBean.getRecords(), SendBean.Records.class);
                adapter = new SendAdapter(getActivity() ,records);
                send_lv.setAdapter(adapter);
            }
        }else if (url.equals(UrlConstant.REBUILD_URL)){
            showToastx(o.getMsg());
        }
    }
}
